using Blog.Command.Extensions;
using Blog.WebApi.Command.Helper;
using Microsoft.OpenApi.Models;
using SqlSugar.IOC;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c => 
{
    #region Swagger使用鉴权组件
    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
        Description = "直接在下框中输入Bearer {token}（注意两者之间是一个空格）",
        Name = "Authorization",
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
        {
          {
            new OpenApiSecurityScheme
            {
              Reference=new OpenApiReference
              {
                Type=ReferenceType.SecurityScheme,
                Id="Bearer"
              }
            },
            new string[] {}
          }
        });
    #endregion
});

#region SqlSugar
builder.Services.AddSqlSugar(new IocConfig()
{
  ConnectionString = builder.Configuration.GetSection("SqlConn").Value,
  DbType = IocDbType.SqlServer,
  IsAutoCloseConnection = true
});
#endregion

#region 依赖注入

builder.Services.ExtensionsIOC();

#endregion

#region Jwt鉴权服务

builder.Services.DiscriminateJwt();

#endregion


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

#region Jwt鉴权授权

//添加鉴权管道
app.UseAuthentication();

//授权管道
app.UseAuthorization();

#endregion

app.MapControllers();

app.Run();
